/*
RECDIAL.C

Sound record/playback command dialogs
*/

#include <record.g>
#include <record.rsg>
#include <hwim.h>
#include <p_math.h>
#include <s_.rsg>

GLREF_D PR_RECENG * const receng;
GLREF_D PR_RECWSERV * const w_ws;

LOCAL_C VOID TenthsToDouble(DOUBLE *pdbl,UINT tenths)
/*
Convert 10ths of a second to a double float.
*/
    {
    WORD ten,x;
    DOUBLE c;

    x=tenths;
    p_itof(pdbl,&x);
    ten=10;
    p_itof(&c,&ten);
    p_fdiv(pdbl,&c);
    }

LOCAL_C INT DoubleToTenths(DOUBLE *pdbl)
/*
Convert a double float to 10ths of a second.
*/
    {
    WORD x;
    DOUBLE c;

    x=10;
    p_itof(&c,&x);
    p_fmul(&c,pdbl);
    p_inti(&x,&c);
    return(x);
    }

LOCAL_C VOID SetTenthsFledit(INT item,UINT val)
    {
    DOUBLE dbl;

    TenthsToDouble(&dbl,val);
    hDlgSetFledit(item,&dbl);
    }

LOCAL_C INT SenseTenthsFledit(INT item)
    {
    DOUBLE dbl;

    hDlgSenseFledit(item,&dbl);
    return(DoubleToTenths(&dbl));
    }

#pragma METHOD_CALL

METHOD VOID setrep_dl_dyn_init(PR_SETREP *self)
/*
Dialog for setting repeat count and trailing silence.
*/
    {
    WVEFILE_INFO info;

    p_send3(receng,O_ENG_FILE_INFO,&info);
    hDlgSetNcedit(1,info.repeats);
    SetTenthsFledit(2,info.silence);
    }

METHOD INT setrep_dl_key(PR_SETREP *self,INT index,INT keycode)
/*
Dialog for setting repeat count and trailing silence.
*/
    {
    UINT repeats,silence;

    repeats=hDlgSenseNcedit(1);
    silence=SenseTenthsFledit(2);
    p_send4(receng,O_ENG_SET_REPEATS,repeats,silence);
    return(WN_KEY_CHANGED);
    }

METHOD VOID setpref_dl_dyn_init(PR_SETPREF *self)
/*
Dialog for setting record/playback preferences.
*/
    {
    RECENG_PREFS *pprefs;

    pprefs=(RECENG_PREFS *)p_send2(receng,O_ENG_SENSE_PREFS);
    hDlgSetChlist(1,pprefs->volume);
    SetTenthsFledit(2,pprefs->duration);
    hDlgSetChlist(3,pprefs->disk);
    }

METHOD INT setpref_dl_key(PR_SETPREF *self,INT index,INT keycode)
/*
Dialog for setting record/playback preferences.
*/
    {
    RECENG_PREFS prefs;

    prefs.volume=hDlgSenseChlist(1);
    prefs.disk=hDlgSenseChlist(3);
    prefs.duration=SenseTenthsFledit(2);
    p_send3(receng,O_ENG_SET_PREFS,&prefs);
    return(WN_KEY_CHANGED);
    }

METHOD VOID recnew_dl_dyn_init(PR_RECNEW *self)
/*
The dialog for recording to a new file.
*/
    {
    RECRBUF *prb;

    prb=(RECRBUF *)self->dlgbox.rbuf;
    if (prb->fname[0])
        hDlgSet(1,&prb->fname[0]);
    SetTenthsFledit(3,prb->duration);
    }

METHOD VOID recnewdef_dl_set_size(PR_RECNEWDEF *self)
    {
    p_supersend2(self,O_DL_SET_SIZE);
    hDlgTakeFocus(3);
    }

METHOD INT recnew_dl_key(PR_RECNEW *self,INT index,INT keycode)
/*
All dialogs for recording to a new or existing file share this code.
*/
    {
    RECRBUF *prb;
    INT MaxDuration;

    prb=(RECRBUF *)self->dlgbox.rbuf;
    hDlgSense(1,&prb->fname[0]);
    prb->duration=SenseTenthsFledit(3);
    MaxDuration=p_send3(receng,O_ENG_NEW_FILE,&prb->fname[0]);
    if (MaxDuration<0)
        {
        if (MaxDuration==E_FILE_VOLUME)
            hInfoPrint(NO_RECORD_FLASH);
        else 
            hInfoPrintErr(MaxDuration);
        return(WN_KEY_NO_CHANGE);
        }
    if (prb->duration/10>MaxDuration)
        {
        SetTenthsFledit(3,10*MaxDuration);
        hInfoPrint(NO_SPACE_RESET);
        return(WN_KEY_NO_CHANGE);
        }
    p_send3(w_ws->wserv.cli,O_CL_PAUSE,prb->duration);
    return(WN_KEY_CHANGED);
    }

METHOD VOID recexist_dl_dyn_init(PR_RECEXIST *self)
/*
The dialog for recording to an existing file.
*/
    {
    hDlgSet(1,&((RECRBUF *)self->dlgbox.rbuf)->fname[0]);
    p_supersend2(self,O_DL_DYN_INIT);
    }

METHOD VOID recexist_dl_set_size(PR_RECEXIST *self)
/*
The dialog for recording to an existing file.
*/
    {
    p_supersend2(self,O_DL_SET_SIZE);
    hDlgTakeFocus(3);
    }

METHOD INT recopen_dl_key(PR_RECOPEN *self,INT index,INT keycode)
/*
The dialog for opening a file.
*/
    {
    hDlgSense(1,&((RECRBUF *)self->dlgbox.rbuf)->fname[0]);
    return(WN_KEY_CHANGED);
    }
